package com.retail.proxy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.proxy.entity.request.AdminProxyUpgradeDetailRequest;
import com.retail.proxy.entity.request.SysAdminProxyUpgradeRequest;
import com.retail.proxy.entity.vo.ProxyMechanismCategoryVo;
import com.retail.proxy.entity.vo.ProxyUpgradeConfigVo;
import com.retail.proxy.entity.vo.SysAdminApplyProxyVo;
import com.retail.core.entity.ParamReq;
import com.retail.proxy.entity.request.SysAdminApplyProxyRequest;
import com.retail.proxy.mapper.*;
import com.retail.proxy.service.SysAdminApplyProxyService;
import com.retail.system.entity.vo.AdminVo;
import com.retail.system.mapper.AdminMapper;
import com.retail.team.enums.VerifyFlagEnums;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 申请代理 Service impl
 *
 * @author Administrator
 */
@Service
public class SysAdminApplyProxyServiceImpl implements SysAdminApplyProxyService {

    @Resource
    private SysAdminApplyProxyMapper sysAdminApplyProxyMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private ProxyMechanismCategoryMapper proxyMechanismCategoryMapper;

    @Resource
    private ProxyUpgradeConfigMapper proxyUpgradeConfigMapper;
    @Resource
    private SysAdminProxyUpgradeMapper sysAdminProxyUpgradeMapper;

    @Resource
    private AdminProxyUpgradeDetailMapper adminProxyUpgradeDetailMapper;

    @Override
    public SysAdminApplyProxyVo getModel(Long id) {
        SysAdminApplyProxyVo vo = sysAdminApplyProxyMapper.getModel(id);
        convertAdminName(vo);
        convertProxyOraCategoryName(vo);
        convertProxyCategoryName(vo);
        vo.setVerifyFlagName(VerifyFlagEnums.getStatusValues(vo.getVerifyFlag()));
        vo.setStatusFlagName(StatusFlagEnum.getStatusValues(vo.getStatusFlag()));
        vo.setPayFlagName(DelFlagEnum.getStatusValues(vo.getPayFlag()));
        return vo;
    }


    @Transactional
    @Override
    public Long save(SysAdminApplyProxyRequest sysAdminApplyProxyRequest) {
        sysAdminApplyProxyMapper.save(sysAdminApplyProxyRequest);
        return sysAdminApplyProxyRequest.getId();
    }

    @Transactional
    @Override
    public Long update(SysAdminApplyProxyRequest request) {
        if (ObjectUtil.isNotEmpty(request.getPayMoney())) {
            request.setPayFlag(DelFlagEnum.YES.getValue());
            request.setPayTime(new Date());
        }

        if (!ObjectUtil.equal(request.getVerifyFlag(), VerifyFlagEnums.DAI_SHEN_HE.getValue())) {
            request.setVerifyTime(new Date());
        }

        // 如果审核通过
        if (ObjectUtil.equal(request.getVerifyFlag(), VerifyFlagEnums.SHEN_HE_TONGGUO.getValue())) {
            adminMapper.updateAdminLevelByAdminId(request.getAdminId(), request.getProxyCategoryId());

            saveUpgradeLog(request.getAdminId());

        }

        if (ObjectUtil.equal(request.getVerifyFlag(), VerifyFlagEnums.SHEN_HE_BUTONGGUO.getValue())) {
            adminMapper.updateAdminLevelByAdminId(request.getAdminId(), request.getProxyOraCategoryId());
        }

        sysAdminApplyProxyMapper.update(request);
        return request.getId();
    }


    /**
     * 晋升等级
     *
     * @param adminId
     */
    private void saveUpgradeLog(Long adminId) {
        AdminVo currentUser = adminMapper.getModel(adminId);
        AdminVo parentUser = null;
        if (ObjectUtil.isNotEmpty(currentUser.getParentId())) {
            parentUser = adminMapper.getModel(currentUser.getParentId());
            List<AdminVo> childAdmins = adminMapper.getModelByParentAndAdminLevel(currentUser.getParentId(), currentUser.getAdminLevel());

            ProxyUpgradeConfigVo upgradeConfigVo = proxyUpgradeConfigMapper.getModelByOraCategoryId(currentUser.getAdminLevel());
            if (ObjectUtil.isNotEmpty(childAdmins) && ObjectUtil.isNotEmpty(upgradeConfigVo)) {
                if (childAdmins.size() >= upgradeConfigVo.getUpgradeCount() && parentUser.getAdminLevel() < upgradeConfigVo.getCategoryId()) {
                    SysAdminProxyUpgradeRequest sysAdminProxyUpgradeRequest = new SysAdminProxyUpgradeRequest();
                    sysAdminProxyUpgradeRequest.setAdminId(parentUser.getId());
                    sysAdminProxyUpgradeRequest.setOraCategoryId(parentUser.getAdminLevel());
                    sysAdminProxyUpgradeRequest.setCategoryId(upgradeConfigVo.getCategoryId());
                    sysAdminProxyUpgradeRequest.setVerifyFlag(VerifyFlagEnums.DAI_SHEN_HE.getValue());
                    sysAdminProxyUpgradeRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
                    sysAdminProxyUpgradeRequest.setDelFlag(DelFlagEnum.NO.getValue());

                    sysAdminProxyUpgradeMapper.save(sysAdminProxyUpgradeRequest);

                    Long upgradeId = sysAdminProxyUpgradeRequest.getId();
                    childAdmins.forEach(cadmin -> {
                        AdminProxyUpgradeDetailRequest adminProxyUpgradeDetailRequest = new AdminProxyUpgradeDetailRequest();
                        adminProxyUpgradeDetailRequest.setAdminId(cadmin.getId());
                        adminProxyUpgradeDetailRequest.setUpgradeId(upgradeId);
                        adminProxyUpgradeDetailRequest.setCategoryId(cadmin.getAdminLevel());
                        adminProxyUpgradeDetailRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
                        adminProxyUpgradeDetailRequest.setDelFlag(DelFlagEnum.NO.getValue());
                        adminProxyUpgradeDetailMapper.save(adminProxyUpgradeDetailRequest);
                    });
                }
            }
        }
    }

    @Override
    public List<SysAdminApplyProxyVo> listPage(ParamReq<SysAdminApplyProxyRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<SysAdminApplyProxyVo> res = sysAdminApplyProxyMapper.listPage(params);
        res.forEach(re -> {
            convertAdminName(re);
            convertProxyOraCategoryName(re);
            convertProxyCategoryName(re);
            re.setVerifyFlagName(VerifyFlagEnums.getStatusValues(re.getVerifyFlag()));
            re.setStatusFlagName(StatusFlagEnum.getStatusValues(re.getStatusFlag()));
            re.setPayFlagName(DelFlagEnum.getStatusValues(re.getPayFlag()));
        });

        return res;
    }

    private void convertProxyCategoryName(SysAdminApplyProxyVo re) {
        if (ObjectUtil.isNotEmpty(re.getProxyCategoryId())) {
            ProxyMechanismCategoryVo oraVo = proxyMechanismCategoryMapper.getModel(re.getProxyCategoryId());
            if (ObjectUtil.isNotEmpty(oraVo)) {
                re.setProxyCategoryName(oraVo.getCategoryName());
            }
        }
    }

    private void convertProxyOraCategoryName(SysAdminApplyProxyVo re) {
        if (ObjectUtil.isNotEmpty(re.getProxyOraCategoryId())) {
            ProxyMechanismCategoryVo oraVo = proxyMechanismCategoryMapper.getModel(re.getProxyOraCategoryId());
            if (ObjectUtil.isNotEmpty(oraVo)) {
                re.setProxyOraCategoryName(oraVo.getCategoryName());
            }
        }
    }

    @Override
    public Integer countPage(ParamReq<SysAdminApplyProxyRequest> params) {
        return sysAdminApplyProxyMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return sysAdminApplyProxyMapper.updateDelFlagById(id, delFlag);
    }

    private void convertAdminName(SysAdminApplyProxyVo vo) {
        if (ObjectUtil.isNotEmpty(vo.getAdminId())) {
            AdminVo adminVo = adminMapper.getModel(vo.getAdminId());
            vo.setAdminName(adminVo.getName());
        }
    }

    @Override
    public SysAdminApplyProxyVo getModelByAdminIdAndVerifyFlag(Long adminId, Integer verifyFlag) {
        return sysAdminApplyProxyMapper.getModelByAdminIdAndVerifyFlag(adminId,verifyFlag);
    }
}
